<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\SmartTagRequest;
use App\CustHub\Request\SmartTagRuleRequest;
use App\CustHub\Service\ChSmartTagRuleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 智能标签规则
 */
#[Controller(prefix: 'ch/smartTagRule'),ApiAuth,VerifySignature]
class SmartTagRuleController extends BaseController
{
    #[Inject]
    protected ChSmartTagRuleService $service;

    /**
     * 行为标签组
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('behaviorTagGroups'),ChPermission('ch:str:behavior:tag:groups')]
    public function behaviorTagGroups(): \Psr\Http\Message\ResponseInterface
    {
        $result = $this->service->behaviorTagGroups($this->request->all());
        return $this->success($result);
    }


    /**
     * 标签组列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('tagGroups'),ChPermission('ch:str:tag:groups')]
    public function tagGroups(): \Psr\Http\Message\ResponseInterface
    {
        $result = $this->service->tagGroups($this->request->all());
        return $this->success($result);
    }

    /**
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('enableTagGroup'),ChPermission('ch:str:behavior:tag:enable')]
    public function enableTagGroup(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $result = $this->service->enableTagGroup($request->all());
        return $result ? $this->success() : $this->error('操作失败');
    }

    #[PostMapping("ruleList")]
    public function ruleList(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->ruleList($request->all()));
    }

    /**
     * 获取标签列表
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("getTagList")]
    public function getTagList(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getTagList($request->all()));
    }

    /**
     * 创建规则
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('createRule'),ChPermission('ch:str:create:rule')]
    public function createRule(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $this->service->createRule($request->all());
        return $this->success();
    }

    /**
     * 获取规则信息
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getRuleInfo'),ChPermission('ch:str:rule:info')]
    public function getRuleInfo(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $info = $this->service->getRuleInfo($request->all());
        return $this->success($info);
    }

    /**
     * 更新规则
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('updateRule'),ChPermission('ch:str:update:rule')]
    public function updateRule(SmartTagRuleRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $rst = $this->service->updateRule($request->all());
        return $rst ? $this->success() : $this->error('更新失败');
    }


    #[PostMapping('ruleItem')]
    public function ruleItem(SmartTagRuleRequest $request)
    {
        return $this->success($this->service->newRuleItem($request->all()));
    }

    /**
     * 删除规则
     * @param SmartTagRuleRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delRule')]
    public function delRule(SmartTagRuleRequest $request)
    {
        return $this->service->delRule($request->all()) ?$this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * 转换配置
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('transformCondItems')]
    public function transformCondItems(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->transformConfig($this->request->all()));
    }
}